﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using XCore.Redis;
using XCore.Dependency;
using XCore.Helper;
using SaaS.Model.Dto.Systems;

namespace SaaS.Cache.Auths
{
    /// <summary>
    /// system授权缓存
    /// </summary>
    public class SystemAuthCacheService : AuthCacheServiceBase, ISingletonDependency
    {
        /// <summary>
        /// 总管理员登录Token
        /// </summary>
        private const string SYSTEM_AUTH_TOKEN = "SYSTEM_AUTH_TOKEN";
        /// <summary>
        /// 角色权限
        /// </summary>
        private const string ROLE_PERMISSIONS = "ROLE_PERMISSIONS_";
        /// <summary>
        /// 总管理员信息
        /// </summary>
        private const string SYSTEM_ADMIN_INFO = "SYSTEM_ADMIN_INFO:";
        /// <summary>
        /// 保存管理员名称与ID之间的关系
        /// </summary>
        private const string SYSTEM_ADMIN_USERNAME = "SYSTEM_ADMIN_USERNAME";



        #region ============登录授权================

        /// <summary>
        /// 设置token
        /// </summary>
        /// <param name="adminId"></param>
        /// <param name="token"></param>
        public void SaveSystemToken(int adminId, string token)
        {
            MemoryHelper.Set(SYSTEM_AUTH_TOKEN + adminId, TimeSpan.FromHours(12), token);
            base.SetToken(SYSTEM_AUTH_TOKEN, adminId, token);
        }

        /// <summary>
        /// 检查token
        /// </summary>
        /// <param name="adminId"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool CheckSystemToken(int adminId, string token)
        {
            return token == GetSystemToken(adminId);
        }
        /// <summary>
        /// 获取token
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns></returns>
        public string GetSystemToken(int adminId)
        {
            return MemoryHelper.Get(SYSTEM_AUTH_TOKEN + adminId, TimeSpan.FromHours(12), () =>
             {
                 return base.GetToken(SYSTEM_AUTH_TOKEN, adminId);
             });
        }
        /// <summary>
        /// 移除Token
        /// </summary>
        /// <param name="adminId"></param>
        public void RemoveSystemToken(int adminId)
        {
            MemoryHelper.Remove(SYSTEM_AUTH_TOKEN + adminId);
            base.RemoveToken(SYSTEM_AUTH_TOKEN, adminId);
        }
        #endregion

        #region ============功能权限===============

        /// <summary>
        /// 设置权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="permissions"></param>
        public void SaveRolePermission(int roleId, IEnumerable<string> permissions)
        {
            MemoryHelper.Set(ROLE_PERMISSIONS + roleId, permissions);
            base.SetPermission(ROLE_PERMISSIONS, roleId, permissions);
        }
        /// <summary>
        /// 检查是否拥有此权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="permission"></param>
        /// <returns></returns>
        public bool IsPermission(int roleId, string permission)
        {
            return base.IsPermission(ROLE_PERMISSIONS, roleId, permission);
        }
        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public IEnumerable<string> GetRolePermission(int roleId)
        {
            return MemoryHelper.Get(ROLE_PERMISSIONS + roleId, () =>
            {
                return base.GetPermission(ROLE_PERMISSIONS, roleId);
            });
        }

        #endregion

        #region ===========管理员信息=============
        /// <summary>
        /// 保存总管理员信息
        /// </summary>
        /// <param name="model"></param>
        public void SaveSystemAdminInfo(SystemAdminModel model)
        {
            string key = SYSTEM_ADMIN_INFO + model.AdminId;
            IBatch batch = Redis.CreateBatch();
            batch.HashSetAsync(key, model);
            batch.HashSetAsync(SYSTEM_ADMIN_USERNAME, model.UserName, model.AdminId);
            batch.Execute();
        }
        /// <summary>
        /// 获取总管理员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SystemAdminModel GetSystemAdminInfo(int id)
        {
            string key = SYSTEM_ADMIN_INFO + id;
            return Redis.HashGetAll(key);
        }
        /// <summary>
        /// 通过用户名获取adminId
        /// </summary>
        /// <param name="username"></param>
        /// <param name="adminId"></param>
        /// <returns></returns>
        public int CheckUserName(string username)
        {
            RedisValue value = Redis.HashGet(SYSTEM_ADMIN_USERNAME, username);
            if (value.IsNull) return 0;
            return value.GetRedisValue<int>();
        }

        #endregion



    }
}
